<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.Iterator" %>

<%@ page import ="com.easyattendance.services.AddTeacherService" %>
<%@ page import ="com.easyattendance.model.Teacher" %>
<%@ page import ="com.easyattendance.services.AddClassService" %>
<%@ page import ="com.easyattendance.model.Class" %>
<%@ page import ="com.easyattendance.services.AddSubjectService" %>
<%@ page import ="com.easyattendance.model.Subject" %>
<%@ page import ="com.easyattendance.model.SubjectClass" %>
<%@ page import ="com.easyattendance.model.Assignment" %>
<%@ page import ="com.easyattendance.services.AddAssignmentService" %>

<html>
<head>
<title>Teacher - Reports</title>
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<jsp:include page="../includeFiles.jsp" ></jsp:include>

<link rel="stylesheet" href="../css/ui/jquery-ui-1.10.4.custom.min.css">
<script src="<%=request.getContextPath()%>/public/js/ui/jquery.ui.core.js"></script>
<script src="<%=request.getContextPath()%>/public/js/ui/jquery.ui.widget.js"></script>
<script src="<%=request.getContextPath()%>/public/js/ui/jquery.ui.datepicker.js"></script>

</head>

<!--
		Note: Set the body element's class to "left-sidebar" to position the sidebar on the left.
		Set it to "right-sidebar" to, you guessed it, position it on the right.
	-->
<body class="left-sidebar" onload="getValues()">

<!-- Session check : user logged in or not/ press back button -->
<jsp:include page="../common/checkSession.jsp"></jsp:include>
<jsp:include page="../common/checkTeacherRole.jsp" />

<%	String currentUser = (String) session.getAttribute("currentUser");
	AddTeacherService addTeacherService=new AddTeacherService();
	String teacher_id=addTeacherService.getTeacherByEmailId(currentUser).getTeacher_id();
	
	AddSubjectService addSubjectService = new AddSubjectService();	
%>
	
	<!-- Wrapper -->
	<div id="wrapper">

		<!-- Content -->
		<div id="content">
			<div id="content-inner">
				<header>
						<span class="error_msg">${assignment_list_not_found}</span>
						
					<!-- Select Report Type -->
					<span class="byline">Select type to get particular reports.</span>
					<div id="report_type">
						<input type="radio" name="report_type" id="Assignment-wise" value="Assignment-wise" tabindex="1" checked>Assignment-wise
						<input type="radio" name="report_type" id="Subject-wise" value="Subject-wise">Subject-wise	
					</div>
					
					<!-- Get assignment-wise submission reports for students -->
					<form id="assignment_report" class="get_report" action="${pageContext.request.contextPath}/GetAssignmentReportServlet" method="post">
						<!-- Hidden field -->
							<input type="hidden" name="report_type" value="assignmentwise">
							
						<select id="ass_select_class" name="ass_select_class">
							<option value="" disabled="disabled">Select Class
								<%
								AddClassService addClassService = new AddClassService();	
								List<SubjectClass> classList = new ArrayList<SubjectClass>();
								classList = addClassService.listClassesByTeacherId(teacher_id);
								SubjectClass tempClass = null;
								Iterator classItr = classList.iterator();
																		
							while(classItr.hasNext()) {
							%>
								<%
									tempClass = (SubjectClass) classItr.next();
									String classId = tempClass.getClass_id();
										%>
										<option value='<%=classId %>'><%=classId%>
									<%
										}
									%>
						</select>
						
						<% String classSelected =(String) request.getParameter("class"); %>
						
						<input type="hidden" name="class_selected" value="<%=classSelected%>">
						
						<% String subjectSelected=null,subjectId=null; %>
						<% if(classSelected!=null){ %>
						<select id="ass_select_subject" name="ass_select_subject" value="<script>getCookie("ass_select_subject")</script>">
							<option value="" disabled="disabled">Select Subject
								<% 
									List<Subject> subjectList = new ArrayList<Subject>();
									subjectList = addSubjectService.listSubjectsByTeacherIdForClass(teacher_id, classSelected);
									String tempSubject = null;
									Iterator subjectItr = subjectList.iterator();
								
									while(subjectItr.hasNext()) { %>
										<% tempSubject = (String) subjectItr.next();
											String subjectName = addSubjectService.getSubjectBySubjectId(tempSubject).getSubject_name(); %>
										<option value='<%= subjectName %>'><%= subjectName %>
									<% } %>
						</select>
							 <%  subjectSelected =(String) request.getParameter("subject");%>
							 <input type="hidden" name="subject_selected" value="<%=subjectSelected%>">
						<%} %>
						
						<% if(classSelected!=null && subjectSelected!=null){ %>
							 
						    	<% subjectId=addSubjectService.getSubjectIdBySubjectName(subjectSelected);	 %>
						    			   	 
						<select id="ass_select_assignment" name="ass_select_assignment" value="<script>getCookie("ass_select_assignment")</script>">
							<option value="" disabled="disabled">Select Assignment
								<% AddAssignmentService addAssignmentService = new AddAssignmentService();	
									List<Assignment> assignmentList = new ArrayList<Assignment>();
									assignmentList = addAssignmentService.listAssignmentsBySubject(subjectId,classSelected);
									Assignment tempAssignment = null;
									Iterator AssItr = assignmentList.iterator();
								
									while(AssItr.hasNext()) { %>
										<% tempAssignment = (Assignment) AssItr.next();
											String assignment_name = tempAssignment.getAssignment_name(); %>
										<option value='<%= assignment_name %>'><%= assignment_name %>
									<% } %>
						</select>
						
						<%  String assignmentSelected =(String) request.getParameter("assignment"); %>
							<input type="hidden" name="assignment_selected" value="<%=assignmentSelected%>">
							<input type="submit" value="Generate" class="button">
						<%} %>	
					</form>
					
					<form id="subjectwise_report" class="get_report" action="${pageContext.request.contextPath}/GetAssignmentReportServlet" method="post">
						<!-- Hidden field -->
							<input type="hidden" name="report_type" value="subjectwise">
							
						<select id="sub_select_class" name="sub_select_class">
							<option value="" disabled="disabled">Select Class
								<%
								//AddClassService addClassService = new AddClassService();	
								List<SubjectClass> sub_classList = new ArrayList<SubjectClass>();
								classList = addClassService.listClassesByTeacherId(teacher_id);
								SubjectClass sub_tempClass = null;
								Iterator classItr1 = classList.iterator();
																		
							while(classItr1.hasNext()) {
							%>
								<%
									sub_tempClass = (SubjectClass) classItr1.next();
									String classId = sub_tempClass.getClass_id();
										%>
										<option value='<%=classId %>'><%=classId%>
									<%
										}
									%>
						</select>
						
						<% String sub_classSelected =(String) request.getParameter("class"); %>
						<input type="hidden" name="report_type" value="subjectwise">
						<input type="hidden" name="class_selected" value="<%=sub_classSelected%>">
						
						<% String subSelected=null,subId=null; %>
						<% if(sub_classSelected!=null){ %>
						<select id="sub_select_subject" name="sub_select_subject" value="<script>getCookie("sub_select_subject")</script>">
							<option value="" disabled="disabled">Select Subject
								<% 
									List<Subject> subjectList = new ArrayList<Subject>();
									subjectList = addSubjectService.listSubjectsByTeacherIdForClass(teacher_id, classSelected);
									String tempSubject = null;
									Iterator subjectItr = subjectList.iterator();
								
									while(subjectItr.hasNext()) { %>
										<%  tempSubject = (String) subjectItr.next();
											String subjectName = addSubjectService.getSubjectBySubjectId(tempSubject).getSubject_name(); %>
										<option value='<%= subjectName %>'><%= subjectName %>
									<% } %>
						</select>
							 <%  subjectSelected =(String) request.getParameter("subject");%>
							 <input type="hidden" name="subject_selected" value="<%=subjectSelected%>">
							 <input type="hidden" name="teacher_id" value="<%=teacher_id%>">
							<input type="submit" value="Generate" class="button">
						<%} %>

					</form>
					
					
				</header>
				<div id="login_content">
						
				</div>
				
			</div>
		</div>

		<!-- Sidebar -->
		<div id="sidebar">

			<!-- Sidebar Header - Logo & Name -->
			<jsp:include page="../common/sidebarHeader.jsp" ></jsp:include>

			<jsp:include page="teacherMenu.jsp" />
							
			<!-- Sidebar Footer Content -->
			<jsp:include page="../common/sidebarFooter.jsp" />

		</div>

	</div>
	<script type="text/javascript">
		$(function(){
			$("#assignmentReports").addClass("current_page_item");
		});
		var contextPath='<%=request.getContextPath()%>';
	</script>
	
	<script type="text/javascript">
	$("#subjectwise_report").hide();
	$("#assignment_report").show();
	$("input[name='report_type']").click(function(){
		var report_type = $("input[name='report_type']:checked").val();
		if(report_type == "Assignment-wise") {
			$("#subjectwise_report").hide();
			$("#assignment_report").show();
		}else{
			$("#assignment_report").hide();
			$("#subjectwise_report").show();
		}
	});
</script>
	
	<!--  For drop down values -->
</script>
	<script type="text/javascript">
	
		$(function () {
		    var temp = 0;
		    $('#ass_select_class').click(function () {        
		    	temp++;
		        if (temp == 2) {
		            $(this).change();
		            temp = 0;
		        }         
		    }).change (function () {
		    	var select_class = $("#ass_select_class").val();
				setCookie("ass_select_class",select_class);
				setCookie("is_subjectwise","no");
				window.location.replace(contextPath + "/views/teacher/assignmentReports.jsp?class=" + select_class);
		    });      
		});
		
		$(function () {
		    var temp = 0;
		    $('#sub_select_class').click(function () {        
		    	temp++;
		        if (temp == 2) {
		            $(this).change();
		            temp = 0;
		        }         
		    }).change (function () {
		    	var select_class = $("#sub_select_class").val();
				setCookie("sub_select_class",select_class);
				setCookie("is_subjectwise","1");
				window.location.replace(contextPath + "/views/teacher/assignmentReports.jsp?class=" + select_class);
		    });      
		});
		
		
		$(function () {
		    var temp = 0;
		    $('#ass_select_subject').click(function () {        
		    	temp++;
		        if (temp == 2) {
		            $(this).change();
		            temp = 0;
		        }         
		    }).change (function () {
		    	var select_class = $("#ass_select_class").val();
				var select_subject = $("#ass_select_subject").val();
				setCookie("ass_select_subject",select_subject);
				setCookie("is_subjectwise","no");
				window.location.replace(contextPath + "/views/teacher/assignmentReports.jsp?" + "class=" + select_class + "&subject=" + select_subject);
		    });      
		});
		
		$(function () {
		    var temp = 0;
		    $('#sub_select_subject').click(function () {        
		    	temp++;
		        if (temp == 2) {
		            $(this).change();
		            temp = 0;
		        }         
		    }).change (function () {
		    	var select_class = $("#sub_select_class").val();
				var select_subject = $("#sub_select_subject").val();
				setCookie("sub_select_subject",select_subject);
				setCookie("is_subjectwise","1");
				window.location.replace(contextPath + "/views/teacher/assignmentReports.jsp?" + "class=" + select_class + "&subject=" + select_subject);
		    });      
		});
		
		$(function () {
		    var temp = 0;
		    $('#ass_select_assignment').click(function () {        
		    	temp++;
		        if (temp == 2) {
		            $(this).change();
		            temp = 0;
		        }         
		    }).change (function () {
		    	var select_class = $("#ass_select_class").val();
				var select_subject = $("#ass_select_subject").val();
				var select_assignment = $("#ass_select_assignment").val();
				setCookie("ass_select_assignment",select_assignment);
				setCookie("is_subjectwise","no");
				window.location.replace(contextPath + "/views/teacher/assignmentReports.jsp?" + "class=" + select_class + "&subject=" + select_subject+"&assignment=" + select_assignment);
		    });      
		});
		
				
		function getValues(){
			$("#ass_select_class").val(getCookie("ass_select_class"));
			$("#ass_select_subject").val(getCookie("ass_select_subject"));
			$("#ass_select_assignment").val(getCookie("ass_select_assignment"));
			$("#sub_select_class").val(getCookie("sub_select_class"));
			$("#sub_select_subject").val(getCookie("sub_select_subject"));
			var isSubjectwise = getCookie("is_subjectwise");
			if(isSubjectwise == "1"){
				$("#assignment_report").hide();
				$("#subjectwise_report").show();
				document.getElementById("Subject-wise").checked = true;
			}
		}
		
		function getCookie(name) { 
			var re = new RegExp(name + "=([^;]+)"); 
			var value = re.exec(document.cookie);
			return (value != null) ? unescape(value[1]) : null; 
		}
		
		function setCookie(name, value) {
			  var today = new Date(); 
			  var expiry = new Date(today.getTime() + 1 * 24 * 3600 * 1000); // plus 30 days
			  document.cookie=name + "=" + escape(value) + "; path=/; expires=" + expiry.toGMTString();
		 }
		
		function clearCookie(){
			deleteCookie("ass_select_class");
			deleteCookie("ass_select_subject");
			deleteCookie("ass_select_assignment");
			deleteCookie("sub_select_class");
			deleteCookie("sub_select_subject");
			deleteCookie("is_subjectwise");
			alert("Cookies Deleted");
		}
		
		function deleteCookie(name)
		{
			var expired = new Date(today.getTime() - 24 * 3600 * 1000); // less 24 hours
			document.cookie=name + "=null; path=/; expires=" + expired.toGMTString();
		}		
	</script>
	
</body>
</html>